<?php 
use think\facade\Db;

class im{
    public $ws = '';
    protected static $redis = '';
    
    public function __construct(){
        self::$redis = new Redis();
        self::$redis->connect('127.0.0.1', 6379);
        $this->ws = new swoole_websocket_server("0.0.0.0", 9501,SWOOLE_PROCESS,SWOOLE_SOCK_TCP | SWOOLE_SSL);
        $this->ws->set(
            [
                'ssl_key_file' => __DIR__.'/../key/4399746_rabbitmq.mveris.cn.key',
                'ssl_cert_file' => __DIR__.'/../key/4399746_rabbitmq.mveris.cn.pem', 
            ]
        );
        $this->onOpen();
        
        $this->onMessage();
        
        
        $this->onClose();
        
        $this->ws->start();
        
    }
    public function onMessage(){
        
          //监听WebSocket消息事件
        $this->ws->on('message', function (\Swoole\WebSocket\Server $ws, $frame) {
            $param = (array)(json_decode($frame->data)); 
             
            $fd = $frame->fd;
            switch($param['act']){
                case 'userLogin':
                    $userLogin = new userLogin();
                    $info = $userLogin->TODO($ws,$frame,$fd);
                    $ws->push($fd,json_encode($info) );
                break;
                 case 'userRegister':
                    $userRegister = new userRegister();
                    $info = $userRegister->TODO($frame,$fd);
                    $ws->push($fd,json_encode($info));
                break; 
                case 'confirmAdd':
                    $confirmAdd = new confirmAdd();
                    $info = $confirmAdd->TODO($ws,$frame,$fd,$ws);
                    $ws->push($fd,json_encode($info) );
                break;
                case 'searchContacts':
                    $searchContacts = new searchContacts();
                    $info = $searchContacts->TODO($frame,$fd);
                    $ws->push($fd,json_encode($info) );
                break;
                case 'completeRecord':
                    $completeRecord = new completeRecord();
                    $info = $completeRecord->TODO($frame,$fd);
                    $ws->push($fd,json_encode($info) );
                break;
                case 'Auditors':
                    $Auditors = new Auditors();
                    $info = $Auditors->TODO($ws,$frame,$fd);
                    $ws->push($fd,json_encode($info) );
                break;
                case 'chatLine':
                    $chatLine = new chatLine();
                    $info = $chatLine->TODO($frame,$fd);
                    $ws->push($info['fd'],json_encode($info) );
                break;
                case 'faceToChat':
                    $faceToChat = new faceToChat();
                    $info = $faceToChat->TODO($ws,$frame,$fd);
                    $ws->push($info['fd'],json_encode($info) );
                break;
                case 'getChatLog':
                    $getChatLog = new getChatLog();
                    $info = $getChatLog->TODO($frame,$fd);
                    $ws->push($fd,json_encode($info) );
                break;
 
                    
            }
        });
      
       
    }
    public function onOpen(){
              //监听WebSocket连接打开事件
      $this->ws->on('open', function (\Swoole\WebSocket\Server $ws, $request) {  
            self::$redis->sAdd("userLogin", $request->fd); 
            $this->ws->push($request->fd, json_encode(['act'=>'open','status'=>1])); 
        });
        

    }
    public function onClose(){
        
                //监听WebSocket连接关闭事件
        $this->ws->on('close', function (\Swoole\WebSocket\Server $ws, $fd) {
            $res = Db::name('user')->where('fd',$fd)->update(['fd'=>'']);
            var_dump($res);
            self::$redis->sRem("userLogin",$fd);
            echo "client-{$fd} is closed\n";
        });
    }
}